package com.lesso.lzh.sunflower.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.core.ApiResponse;
import com.lesso.lzh.sunflower.pojo.dto.EmployeeDTO;
import com.lesso.lzh.sunflower.pojo.dto.EmployeeQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.EmployeeVO;
import com.lesso.lzh.sunflower.service.IEmployeeService;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.DubboReference;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import com.lesso.lzh.sunflower.resource.config.RabbitMQConfig;
import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 员工基础信息 前端控制器
 * </p>
 *
 * @author zqz
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/base/employee")
public class EmployeeController {
    @DubboReference
    private IEmployeeService employeeService;

    @PostMapping
    public ApiResponse<Long> save(@Valid @RequestBody EmployeeDTO dto) {
        try {
            return ApiResponse.<Long>ok().data(employeeService.save(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Long>error().msg("保存员工信息失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/page")
    public ApiResponse<Page<EmployeeVO>> page(EmployeeQueryDTO dto) {
        try {
            return ApiResponse.<Page<EmployeeVO>>ok()
                    .data(employeeService.page(dto))
                    .build();
        } catch (Exception e) {
            return ApiResponse.<Page<EmployeeVO>>error().msg("分页查询员工信息失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<EmployeeVO>> customPage(EmployeeQueryDTO dto) {
        try {
            return ApiResponse.<Page<EmployeeVO>>ok().data(employeeService.customPage(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Page<EmployeeVO>>error().msg("自定义分页查询员工信息失败: " + e.getMessage()).build();
        }
    }

    @PutMapping
    public ApiResponse<Object> update(@RequestBody EmployeeDTO dto) {
        try {
            employeeService.update(dto);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("更新员工信息失败: " + e.getMessage()).build();
        }
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        try {
            employeeService.deleteById(id);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("删除员工信息失败: " + e.getMessage()).build();
        }
    }

    @GetMapping
    public ApiResponse<Object> selectById(@RequestParam("id") Long id) {
        try {
            Object employee = employeeService.selectById(id);
            return ApiResponse.ok().data(employee).build();
        } catch (Exception e) {
            return ApiResponse.error().msg("查询员工信息失败: " + e.getMessage()).build();
        }
    }

    @NacosValue(value = "${hello.name}", autoRefreshed = true)
    private String nacosValue;

    private final StringRedisTemplate stringRedisTemplate;
    private final RedissonClient redissonClient;
    private final AmqpTemplate amqpTemplate;

    public EmployeeController(StringRedisTemplate stringRedisTemplate,
                              RedissonClient redissonClient,
                              AmqpTemplate amqpTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.redissonClient = redissonClient;
        this.amqpTemplate = amqpTemplate;
    }

    @GetMapping("/nacos")
    public ApiResponse<String> nacos() {
        try {
            return ApiResponse.<String>ok().msg(nacosValue).build();
        } catch (Exception e) {
            return ApiResponse.<String>error().msg("获取Nacos配置失败: " + e.getMessage()).build();
        }
    }

    @SneakyThrows
    @GetMapping("/redis")
    public ApiResponse<String> redis() {
        RLock lock = redissonClient.getLock("redis-lock-1");
        try {
            if (lock.isLocked()) {
                return ApiResponse.<String>error().msg("当前事务正在处理中").build();
            }
            lock.lock(3, TimeUnit.SECONDS);
            String hello = stringRedisTemplate.opsForValue().get("hello");
            return ApiResponse.<String>ok().data(hello).build();
        } catch (Exception e) {
            return ApiResponse.<String>error().msg("Redis操作失败: " + e.getMessage()).build();
        } finally {
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

    @GetMapping("/mq")
    public ApiResponse<String> mq(@RequestParam("content") String content) {
        try {
            amqpTemplate.convertAndSend(RabbitMQConfig.SUNFLOWER_EXCHANGE, RabbitMQConfig.SUNFLOWER_ROUTING_KEY, content);
            return ApiResponse.<String>ok().build();
        } catch (Exception e) {
            return ApiResponse.<String>error().msg("消息发送失败: " + e.getMessage()).build();
        }
    }
}